// pageA/classify.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noMore:false,
    classifyList:[],
    product:[],
    productName:[],
    goods_cate:[],
    classifyTitle:"",
    index:0,
    cate_id:"",
    // 分页
    p:"",
    scrollTop:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    
    wx.getStorage({
      key: 'idx',
      success: function(res) {
        var index = res.data;
        if(index == undefined){
          index = 0;
        }
        that.getGoodsList(index,function(cate){
          that.getGoods(cate,index)
        })
        
      }
    })
  },
  /**
   * 获取分类列表
   */
  getGoodsList:function(index,callback){
    var that = this;
    var shop_id = app.globalData.shop_id;
    var p = 1;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Category/cateList',
      data: {
        shop_id: shop_id
      },
      header: {
        "content-type": "application/json"
      },
      success:function(res){
        var classifyList = res.data.data;
        classifyList[index]['checked'] = true;
        // 初始化默认选择分类第一个，获取分类产品
        var cate_id = classifyList[index].cate_id;
        that.setData({
          classifyList: classifyList,
          cate_id: cate_id,
          p:p
        })
        callback(cate_id,index)
      }
    })
  },
  /**
   * 获取分类物品
   */
  getGoods:function(cate_id,index){
    var that = this;
    var shop_id = app.globalData.shop_id;
    var p = p;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Category/cateGoodsList',
      data: {
        cate_id: cate_id,
        shop_id: shop_id,
        p: 1
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        var product = res.data.data.goods_list;
        var goods_cate = res.data.data.goods_cate;
        var classifyTitle = goods_cate[index].category;
        that.setData({
          cate_id: cate_id,
          product: product,
          classifyTitle: classifyTitle,
          goods_cate: goods_cate
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.name,
      path: "/pages/shouye/shouye",
      imageUrl: app.globalData.headPic
    }
  },
  /**
   * 切换分类
   */
  changeClassify:function(e){
    var index = e.currentTarget.dataset.index;
    var goods_cate = this.data.goods_cate;
    var classifyList = this.data.classifyList;
   //切换选中状态 
    classifyList.map(function(val){
      val["checked"] = false;
      return val;
    })
    classifyList[index]['checked'] = true;
    // 改变当前选中分类的名称
    var classifyTitle = classifyList[index].category;
    var cate_id = classifyList[index].cate_id;
    var shop_id = app.globalData.shop_id;
    var p = 1;
    // 获取当前分类下的物品
    this.getGoods(cate_id,index)
    this.setData({
      classifyList: classifyList,
      noMore:false,
      scrollTop:0
    })
  },
  /**
   * 获取下个分页
   */
  most:function(){
    var noMore = this.data.noMore;
    if(noMore == false){
      var p = this.data.p;
      p = p + 1;
      var that = this;
      var cate_id = this.data.cate_id;
      var shop_id = app.globalData.shop_id;
      var products = this.data.product;
      wx.request({
        url: app.globalData.yuming + 'merchant.php/Api/Category/cateGoodsList',
        data: {
          cate_id: cate_id,
          shop_id: shop_id,
          p: p
        },
        header: {
          "content-type": "application/json"
        },
        success: function (res) {
          var product = res.data.data.goods_list;
          if(product != ""){
            products.push(product)
          }else{
           noMore=true
          }
          that.setData({
            p:p,
            product: products, 
            noMore: noMore
          })
        }
      })
    }
  },
  /**
   * 商品详情
   */
  detail:function(e){
    var index = e.currentTarget.dataset.index;
    var product = this.data.product;
    console.log(product)
    app.globalData.productModel = product[index];
    wx.navigateTo({
      url: '/pageA/detail/detail',
    })
  }
})